from django.http import HttpResponse, HttpResponseRedirect
from django.utils.translation import ugettext as _
from django.shortcuts import render_to_response
#from ragendja.template import render_to_response
from django.template import RequestContext
from django.contrib.auth.models import User,AnonymousUser
from django.views.decorators.cache import cache_page
from google.appengine.ext import db

from realpython.blog.models import *
from realpython.index.functions import *
#from blog.forms import BlogForm

#@cache_page(60)
def index(request):
    blogs = Blog.get_blog_list(None, request.GET.get('page', '1'))
    temvar = {'pager': blogs}
    return render_to_response('blog.html', temvar, context_instance=RequestContext(request))


def blog_add(request):
    if not request.user.is_authenticated():
        create_message(request, _('PLease login first!'))
        return HttpResponseRedirect('/blog')
    form = BlogForm()
    if request.method == 'POST':
        form = BlogForm(request.POST)
        if form.is_valid():
            #form.save()
            title = form.cleaned_data['title']
            content = form.cleaned_data['content']
            category = form.cleaned_data['category']
            sender = request.user
            blog = Blog.create(title, content, category, sender)
            create_message(request, _('Thanks for posting blog!'))
            return HttpResponseRedirect('/blog/' + str(blog.key()))
        else:
            create_message(request, _('Error!'), 'error')
    else:
        form = BlogForm()
    
    return render_to_response('blog_add.html', {'form': form}, context_instance=RequestContext(request))
    
def blog_category_list(request, categoryKey):
    category = BlogCategory.get_by_key(categoryKey)
    blogs = Blog.get_blog_list(category, request.GET.get('page', '1'))
    temvar = {'pager': blogs}
    return render_to_response('blog.html', temvar, context_instance=RequestContext(request))
    pass
    
def display_blog_category(request):
    categories = BlogCategory.get_category()
    return {
        'categories': categories,
    }
    
def blog_display(request, key):
    blog = Blog.get_by_key(key)
    #blog = db.get(db.Key(key))
    if request.method == 'POST':
        form = BlogCommentForm(request.POST)
        if form.is_valid():
            content = form.cleaned_data['content']
            if request.user.is_authenticated():
                user = request.user
            else:
                user = None
            comment = BlogComment.create(content, blog, user)
            create_message(request, _('Thanks for posting comments!'))
            form = BlogCommentForm()
        else:
            create_message(request, _('Error!'), 'error')
    else:
        form = BlogCommentForm()
    comments = BlogComment.get_by_blog(blog)
    temvar = {'message': blog, 'comments': comments, 'form':form}
    return render_to_response('blog_display.html', temvar, context_instance=RequestContext(request))
        
    
